package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 新用户注册信息接口：
     * @param param  用户info
     * @param token  token信息
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody Map<String,String> param, @RequestHeader("Authorization") String token) { //返回值：object

        try {
            Boolean bool = userInfoService.savaUserInfo(param, token);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存信息失败！").build();

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }


    /**
     * 用户头像上传接口：
     * @param multipartFile  头像文件
     * @param token  要校验的token
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity<Object> saveLogo(@RequestParam("headPhoto")MultipartFile multipartFile, @RequestHeader("Authorization") String token) { //返回值：object

        try {
            Boolean bool = userInfoService.saveLogo(multipartFile, token);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("保存头像失败！").build();

        return ResponseEntity.status(HttpStatus.OK).body(errorResult);
    }
}
